/******************************************************************************
//             INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in accordance with the terms of that agreement.
//        Copyright (c) 2000-2001 Intel Corporation. All Rights Reserved.
//
//  Description:
//      Intel(R) JPEG library for X platform 
//
********************************************************************************/
#include "_ijx.h"

/******************************************************************************
// Name:            ijHuffmanDecode_Gray_DC
// Description:     Decoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_Gray_DC(Ipp16s **ppMCUBuf, 
                                   int ah,
                                   int al,
                                   IJBITTREAMSTATE *pStream,
                                   IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
    } else {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);

    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanDecode_444_DC
// Description:     Decoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_444_DC(Ipp16s **ppMCUBuf,
                                  int ah,
                                  int al,
                                  IJBITTREAMSTATE *pStream,
                                  IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[1],
                                                    &(pDecodeState->dcPred[1]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[1]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[2],
                                                    &(pDecodeState->dcPred[2]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[2]);
    } else {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[1],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[2],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanDecode_422_DC
// Description:     Decoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_422_DC(Ipp16s **ppMCUBuf, 
                                  int ah,
                                  int al,
                                  IJBITTREAMSTATE *pStream,
                                  IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][64]),
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[1],
                                                    &(pDecodeState->dcPred[1]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[1]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[2],
                                                    &(pDecodeState->dcPred[2]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[2]);
    } else {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][64]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[1],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[2],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/******************************************************************************
// Name:            ijHuffmanDecode_411_DC
// Description:     Decoding scan data (DC) for Progressive mode
// Input Arguments:
//              ah: Indicates approximation high
//              al: Indicates approximation low
//         pStream: Identifies IJBITTREAMSTATE data structure.
//    pDecodeState: Identifies IJDECODESTATE strcuture.
// Output Arguments:
//        ppMCUBuf: Identifies MCU coefficients buffer.
//                          
// Returns:          
//        [Success] IJX_RTCODE_OK
//        [Failure] IJX_RTCODE_BAGARG
//                  IJX_RTCODE_INSUFFICIENTBUF
//                  IJX_RTCODE_ERRPARSE
//                     
//                  
******************************************************************************/
IJXRESULT ijHuffmanDecode_411_DC(Ipp16s **ppMCUBuf, 
                                  int ah,
                                  int al,
                                  IJBITTREAMSTATE *pStream,
                                  IJDECODESTATE *pDecodeState)
{
    IJXRESULT rtCode;
    if (0==ah) {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][64]),
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][128]),
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][192]),
                                                    &(pDecodeState->dcPred[0]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[0]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[1],
                                                    &(pDecodeState->dcPred[1]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[1]);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCFirst_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[2],
                                                    &(pDecodeState->dcPred[2]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al,
                                                    pDecodeState->paIdxDCHuffTable[2]);
    } else {
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[0],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][64]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][128]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif
        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    &(ppMCUBuf[0][192]),
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[1],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
#ifdef _IJX_HUFFMANBADCHK
        if (!IJX_SUCCEEDED(rtCode)) {
            return IJX_RTCODE_ERROR;
        }
#endif

        rtCode=ippiDecodeHuffman_Direct_JPEG_DCRefine_1u16s(pStream->pBitStream,  
                                                    &(pStream->nUsedBufBitsLen),
                                                    ppMCUBuf[2],
                                                    &(pStream->nMarker),
                                                    &(pStream->nPrefetchedBits),
                                                    &(pStream->nNumValidPrefetchedBits),
                                                    al);
    }
    
#ifdef _IJX_HUFFMANBADCHK
    if (!IJX_SUCCEEDED(rtCode)) {
        return IJX_RTCODE_ERROR;
    }
#endif

//    pDecodeState->counter.nScanMCUCounter++;

    if ( (0<pDecodeState->nRstInterval) ){
        pDecodeState->counter.nDRICounter--;
        if (0==pDecodeState->counter.nDRICounter) {
            ijAdjustRestartIntervalInv(pStream, pDecodeState);
        }
    }

    return IJX_RTCODE_OK;
}

/* EOF */
